package com.jia.rpc.server;

import io.vertx.core.Vertx;
import io.vertx.core.VertxBuilder;

/**
 * Vert.x实现web服务器，能够监听制定的端口并处理请求
 */
public class VertxHttpServer implements HttpServer{
    @Override
    public void doStart(int port) {
        //创建vertx实例
        Vertx vertx = Vertx.vertx();

        //创建HTTP服务器
        io.vertx.core.http.HttpServer server = vertx.createHttpServer();

        //监听端口并处理请求
        server.requestHandler(new HttpServerHandler());
//        server.requestHandler(request -> {
//            //处理HTTP请求
//            System.out.println("Received Request:" + request.method() + " " + "request uri:"+ request.uri());
//
//            //发送HTTP响应
//            request.response()
//                    .putHeader("content-type", "text/plain")
//                    .end("Hello from Vert.x HTTP server!");
//        });

        //启动HTTP服务并监听端口
        server.listen(port, result -> {
           if(result.succeeded()) {
               System.out.println("Server is now listening on port:" + port);
           }else {
               System.out.println("Failed to start server:" + result.cause());
           }
        });
    }
}
